package net.ariel.myideablog.model;

import java.util.Calendar;
import java.util.Date;

import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@PersistenceCapable
public class Blog {

	@PrimaryKey
	@Persistent(valueStrategy=IdGeneratorStrategy.IDENTITY)
	private Key key;
	
	@Persistent
	private String title;
	
	@Persistent
	private String body;
	
	@Persistent
	private Date createdAt;
	
	@Persistent
	private String categoryKey;
	
	private Category category;
	
	private String keyString;
	
	private String blogTime;
	
	public Blog() {
		
	}
	
	public Blog(String title, String body) {
		this.title = title;
		this.body = body;
	}
	
	public void setKey(Key key) {
		this.key = key;
	}
	
	public void setTitle(String title) {
		this.title = title;
	}
	
	public void setBody(String body) {
		this.body = body;
	}
	
	public void setCreatedAt(Date createdAt) {
		this.createdAt = createdAt;
	}
	
	public void setCategory(Category category) {
		this.category = category;
	}
	
	public Key getKey() {
		return key;
	}
	
	public String getTitle() {
		return title;
	}
	
	public String getBody() {
		return body;
	}
	
	public Date getCreatedAt() {
		return createdAt;
	}
	
	public void setCategoryKey(String categoryKey) {
		this.categoryKey = categoryKey;
	}
	
	public String getCategoryKey() {
		return categoryKey;
	}
	
	public Category getCategory() {
		return category;
	}
	
	public String getKeyString() {
		return KeyFactory.keyToString(key);
	}
	
	public String getBlogTime() {
		Calendar c = Calendar.getInstance();
		c.setTime(createdAt);
		int month = c.get(Calendar.MONTH);
		String monthStr = null;
		if(month+1<10) {
			monthStr = "0"+Integer.toString(++month);
		}
		else {
			monthStr = Integer.toString(++month);
		}
		return Integer.toString(c.get(Calendar.YEAR))+"-"+monthStr+"-"+Integer.toString(c.get(Calendar.DAY_OF_MONTH));
	}
	
}
